package com.liuxinlong.common.uploadFile;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UploadTest {
    public void readRomateFile(String romatefilename){
        URL urlfile;
        BufferedReader in;
        PrintWriter out;
        String content = "";
        String inputLine;
        try{
            urlfile = new URL(romatefilename);
            in = new BufferedReader(new InputStreamReader(urlfile.openStream()));
            inputLine = in.readLine();
            int i=0;
            while (inputLine != null){
                content += inputLine ;
                inputLine = in.readLine();
                i++;
            }
            System.out.println(i);
            in.close();
        } catch (MalformedURLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void downloadRomateFile(String romateUrl,String localPath,String userName,String password){
        String userPassword =  userName + ":" + password;

        String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());
        URLConnection c;

        FileOutputStream fos = null;
        URL urlfile;
        BufferedReader in;
        PrintWriter out;
        String content = "";
        String inputLine;
        try {
            urlfile = new URL(romateUrl);
            c = urlfile.openConnection();
            c.setRequestProperty ("Authorization", "Basic " + encoding);
            c.connect();
            System.out.println("内容类型: "+c.getContentType());
            System.out.println("内容长度: "+c.getContentLength());
            System.out.println("创建日期: "+new Date(c.getDate()));
            System.out.println("最后修改日期: "+new Date(c.getLastModified()));
            System.out.println("终止日期: "+new Date(c.getExpiration()));

//            InputStream in=c.getInputStream();


//            in = new BufferedReader(new InputStreamReader(urlfile.openStream()));
//            inputLine = in.readLine();
//            int i=0;
//            while (inputLine != null){
//                content += inputLine ;
//                inputLine = in.readLine();
//                i++;
//            }
//            System.out.println(content);
            // 获取上的文件
//            File file = new File(localPath);
//            byte[] bytes = new byte[1024];
//            int i;
//            fos = new FileOutputStream(file);
//            while((i = in.read(bytes)) != -1){
//                fos.write(bytes, 0, i);
//            }
            System.out.println("download success!!");

        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fos!=null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void downloadRomateFile(String romateUrl,String fileName,String localPath) {
        FileOutputStream fos = null;
        URL urlfile;

        //判断文件的保存路径后面是否以/结尾
        if (!localPath.endsWith("/")) {
            localPath += "/";
        }
        InputStream inputStream;
        try {
            urlfile = new URL(romateUrl + fileName);
            URLConnection conn = urlfile.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            // post方式不能使用缓存
            conn.setUseCaches(false);
            //连接指定的资源
            conn.connect();
            //获取网络输入流
            inputStream=conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            //写入到文件（注意文件保存路径的后面一定要加上文件的名称）
            fos = new FileOutputStream(localPath+fileName);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            byte[] buf = new byte[4096];
            int length = bis.read(buf);
            //保存文件
            while(length != -1)
            {
                bos.write(buf, 0, length);
                length = bis.read(buf);
            }
            bos.close();
            bis.close();
            System.out.println("download success!!");

        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fos!=null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void downloadRomateFiles(String romateUrl,String localPath){
        BufferedReader in;
        FileOutputStream fos = null;
        URL urlfile;

        InputStream inputStream;
        String inputLine;
        List<String> fileNameList = new ArrayList<>();
        try {
            urlfile = new URL(romateUrl);
            in = new BufferedReader(new InputStreamReader(urlfile.openStream()));
            inputLine = in.readLine();
            int i=0;
            while (inputLine != null){
                fileNameList.add(inputLine) ;
                inputLine = in.readLine();
                i++;
            }
            //判断文件的保存路径后面是否以/结尾
            if (!localPath.endsWith("/")) {
                localPath += "/";
            }

            for (String fileName :fileNameList) {
                URL url = new URL(romateUrl + fileName);
                URLConnection conn = url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                // post方式不能使用缓存
                conn.setUseCaches(false);
                //连接指定的资源
                conn.connect();
                //获取网络输入流
                inputStream=conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                //写入到文件（注意文件保存路径的后面一定要加上文件的名称）
                fos = new FileOutputStream(localPath+fileName);
                BufferedOutputStream bos = new BufferedOutputStream(fos);

                byte[] buf = new byte[4096];
                int length = bis.read(buf);
                //保存文件
                while(length != -1)
                {
                    bos.write(buf, 0, length);
                    length = bis.read(buf);
                }
                bos.close();
                bis.close();
            }

            System.out.println("download success!!");

        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fos!=null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




}


